﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Business;
using AMM.Model;
using AMM.Model.Business;

namespace AMM.Business.Business {
    public class CustomerOrderProcessor : BaseProcessor {

        private int _pageSize;
        private CustomerOrderRepository _customerOrderRepository;

        public CustomerOrderProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._customerOrderRepository = new CustomerOrderRepository(pageSize);
        }

        public CustomerOrder FetchCustomerOrderById(int customerOrderId) {
            try {
                return this._customerOrderRepository.FetchCustomerOrderById(customerOrderId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定客户订单失败！");
            }
        }

        public List<CustomerOrderDetailsViewModel> FetchCustomerOrderDetailsViewModelList(int customerOrderId) {
            try {
                return this._customerOrderRepository.FetchCustomerOrderByIdForView(customerOrderId).CustomerOrderDetails.Select(x => {
                    return new CustomerOrderDetailsViewModel() {
                        Key = x.CustomerOrderDetailsId,
                        OrderProductName = x.OrderProductName,
                        ProductCode = x.Product.Code,
                        Price = x.Price,
                        Quantity = x.Quantity,
                        TotalMoney = x.TotalMoney,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定客户订单失败！");
            }
        }

        public List<CustomerOrderViewModel> FetchCustomerOrderViewModelList(CustomerOrderSearchModel searchModel, int pageIndex = 1) {
            try {
                List<CustomerOrder> resultList = this._customerOrderRepository.FetchCustomerOrderList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new CustomerOrderViewModel() {
                        Key = x.CustomerOrderId,
                        CustomerCode = x.Customer.Code,
                        CustomerName = x.Customer.Name,
                        DeliverDate = x.DeliverDate,
                        ExchangeRate = x.ExchangeRate,
                        OrderDate = x.OrderDate,
                        OrderNo = x.OrderNo,
                        OrderType = x.Basic2.Code,
                        OrderStatus = MethodHelper.GetEnumsDescription(typeof(AuditStatus), x.OrderStatus),
                        AuditedBy = x.AuditedBy,
                        AuditedDatetime = x.AuditedDatetime,
                        PaymentType = x.Basic1.Name,
                        Remarks = x.Remarks,
                        IsAttach = x.AttachmentId.HasValue,
                        TotalMoney = x.TotalMoney,
                        TotalMoneyInLocal = x.TotalMoneyInLocal,
                        TotalQuantities = x.TotalQuantities,
                        PayCurrency = x.Basic.Name,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取客户订单列表失败！");
            }
        }

        public void Insert(CustomerOrder customerOrder) {
            try {
                customerOrder.CustomerOrderId = Guid.NewGuid().GetHashCode();
                customerOrder.CreatedDatetime = DateTime.Now;
                customerOrder.OrderStatus = (int)AuditStatus.Waiting;
                customerOrder.CustomerOrderDetails.ToList().ForEach(x => {
                    x.CreatedBy = customerOrder.CreatedBy;
                    x.CreatedDatetime = customerOrder.CreatedDatetime;
                    x.CustomerOrderDetailsId = Guid.NewGuid().GetHashCode();
                });
                this._customerOrderRepository.Insert(customerOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新客户订单失败！");
            }
        }

        public void Update(CustomerOrder customerOrder) {
            try {
                CustomerOrder oldCustomerOrder = this.FetchCustomerOrderById(customerOrder.CustomerOrderId);
                if (!oldCustomerOrder.RowVersion.SequenceEqual(customerOrder.RowVersion)) {
                    throw this.BuildException("更新客户订单失败，数据不一致性错误！");
                }
                customerOrder.ModifiedDatetime = DateTime.Now;
                this._customerOrderRepository.UpdateCustomerOrder(customerOrder, oldCustomerOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新客户订单失败！");
            }
        }

        public void Delete(CustomerOrder customerOrder) {
            try {
                this._customerOrderRepository.Delete(customerOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除客户订单失败！");
            }
        }

        public void Audit(CustomerOrder customerOrder) {
            try {
                CustomerOrder oldCustomerOrder = this.FetchCustomerOrderById(customerOrder.CustomerOrderId);
                if (!oldCustomerOrder.RowVersion.SequenceEqual(customerOrder.RowVersion)) {
                    throw this.BuildException("审核客户订单失败，数据不一致性错误！");
                }
                customerOrder.AuditedDatetime = DateTime.Now;
                this._customerOrderRepository.UpdateCustomerOrder(customerOrder, oldCustomerOrder);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("审核客户订单失败！");
            }
        }

        public void Delete(params int[] customerOrderIds) {
            try {
                this._customerOrderRepository.DeleteMultipleCustomerOrder(customerOrderIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除客户订单失败！");
            }
        }
    }
}
